<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\EnginModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\LabourBanModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourHuaListModel;
use app\index\model\LabourHuaModel;
use app\index\model\LabourJieListModel;
use app\index\model\LabourJieModel;
use app\index\model\LabourLingListModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenListModel;
use app\index\model\LabourShenModel;
use app\index\model\LabourWageModel;
use app\index\model\LabourXiangModel;
use app\index\model\LabourYongListModel;
use app\index\model\LabourYongModel;
use app\index\model\UserModel;
use app\index\service\CertService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\LabourService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Labour extends Common
{
    public function list_labour_shen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shen.deleted_user','=', 0];
        $where[] = ['shen.comid','=',$this->userinfo['comid']];
        $where[] = ['shen.isturn','=',1];
        $where[] = ['shen.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|shen.number|shen.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shen.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['shen.caiid','=', $param['caiid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shen.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shenmoney']) && !empty($param['shenmoney'])){
            $where[] = ['shen.shenmoney','like', '%'.$param['shenmoney'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shen.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shen.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourShenModel::alias('shen')
            ->field('shen.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=shen.enginid')
            ->join('contract_cai cai','cai.id=shen.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_shen', 'cai'))
            ->order('shen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_shen');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择采购合同');
            }

            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new LabourService())->create_labour_shen_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入申报明细');
            }


            Db::startTrans();
            try {
                $id = LabourShenModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shenid'] = $id;
                    LabourShenListModel::insertGetId($vv);
                }

                (new OptService())->relation_file($files,'labour_shen',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_shen', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_shen', $id, '劳务进度款申报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new LabourService())->create_labour_shen_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = LabourShenModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入申报明细');
            }

            Db::startTrans();
            try {
                LabourShenModel::update($param);

                LabourShenListModel::where(['shenid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shenid'] = $param['id'];
                    LabourShenListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'labour_shen',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_shen', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_shen', $param['id'], '劳务进度款申报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourShenModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = LabourShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_shen', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_shen(Request $request){
        $param = $request->post();
        $data = LabourShenModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourShenModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_shen', $data['id'], '劳务进度款申报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_shen(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourShenModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $list = LabourShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_shen', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_labour_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jie.number|jie.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['jie.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['fafee']) && !empty($param['fafee'])){
            $where[] = ['jie.fafee','like', '%'.$param['fafee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_jie', 'cai'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择采购合同');
            }

            $param['number'] = (new LabourService())->create_labour_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }


            Db::startTrans();
            try {
                $id = LabourJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    LabourJieListModel::insertGetId($vv);
                }

                (new OptService())->relation_file($files,'labour_jie',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_jie', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_jie', $id, '劳务完工结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new LabourService())->create_labour_jie_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = LabourJieModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }

            Db::startTrans();
            try {
                LabourJieModel::update($param);

                LabourJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    LabourJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'labour_jie',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_jie', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_jie', $param['id'], '劳务完工结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = LabourJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_jie(Request $request){
        $param = $request->post();
        $data = LabourJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_jie', $data['id'], '劳务分包完工结算');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $list = LabourJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_labour_ling(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.isturn','=',1];
        $where[] = ['ling.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|ling.gonger|ling.number|ling.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ling.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ling.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ling.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ling.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ling.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ling.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ling.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourLingModel::alias('ling')
            ->field('ling.*,e.name as enginname')
            ->join('engin e','e.id=ling.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_ling', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_ling');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择劳务班组');
            }

            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new LabourService())->create_labour_ling_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入登记明细');
            }


            Db::startTrans();
            try {
                $id = LabourLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    LabourLingListModel::insertGetId($vv);
                }

                (new OptService())->relation_file($files,'labour_ling',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_ling', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_ling', $id, '零星劳务登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new LabourService())->create_labour_ling_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = LabourLingModel::where(['id'=>$param['id']])->find();


            if(empty($param['gonger'])){
                return outputJson(4000, '请选择劳务班组');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入登记明细');
            }

            Db::startTrans();
            try {
                LabourLingModel::update($param);

                LabourLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    LabourLingListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'labour_ling',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_ling', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_ling', $param['id'], '零星劳务登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = LabourLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_ling', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_ling(Request $request){
        $param = $request->post();
        $data = LabourLingModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourLingModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_ling', $data['id'], '零星劳务登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_ling(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourLingModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = LabourLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_ling', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_labour_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.idcard|ling.tel|ban.name|ling.areas|ling.bankname|ling.banknum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }
        if(isset($param['banid_name']) && !empty($param['banid_name'])){
            $where[] = ['ban.name','like', '%'.$param['banid_name'].'%'];
        }
        if(isset($param['gonglei']) && !empty($param['gonglei'])){
            $where[] = ['ling.gonglei','=', $param['gonglei']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['ling.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['ling.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['ling.banknum','like', '%'.$param['banknum'].'%'];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';

        $data['gonglei'] = (new LabourService())->get_labour_gong_gonglei();
        $data['is_gonglei'] = isset($param['gonglei']) ? $param['gonglei'] : '';


        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*,ban.name as banid_name')
            ->leftJoin('labour_ban ban','ban.id=ling.banid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_gong', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_gong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入姓名');
            }
            if(empty($param['idcard'])){
                return outputJson(4000, '请输入身份证号');
            }
            if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                return outputJson(4000, '身份证号错误');
            }
            if((new LabourService())->check_labour_gong_name($param['name'], $param['idcard'])){
                return outputJson(4000, '人员已存在');
            }


            if(empty($param['tel'])){
                return outputJson(4000, '请输入联系电话');
            }
            if(empty($param['yongxing'])){
                return outputJson(4000, '请选择用工性质');
            }
            if(empty($param['gongtype'])){
                return outputJson(4000, '请选择工种');
            }


            $param['sex'] = check_sex_by_idcard($param['idcard']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = LabourGongModel::insertGetId($param);

                (new OptService())->relation_file($files,'labour_gong',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_gong', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_gong', $id, '工人花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['areas'] = (new OptService())->get_city_cader();
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new LabourService())->get_labour_gong_gonglei();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入姓名');
            }
            if(empty($param['idcard'])){
                return outputJson(4000, '请输入身份证号');
            }
            if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                return outputJson(4000, '身份证号错误');
            }
            if((new LabourService())->check_labour_gong_name($param['name'], $param['idcard'], $param['id'])){
                return outputJson(4000, '人员已存在');
            }
            if(empty($param['tel'])){
                return outputJson(4000, '请输入联系电话');
            }
            if(empty($param['yongxing'])){
                return outputJson(4000, '请选择用工性质');
            }
            if(empty($param['gongtype'])){
                return outputJson(4000, '请选择工种');
            }

            $param['sex'] = check_sex_by_idcard($param['idcard']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                LabourGongModel::update($param);

                (new OptService())->relation_file($files,'labour_gong',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_gong', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_gong', $param['id'], '工人花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourGongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $banid_info = LabourBanModel::get_info_by_pk($data['data']['banid']);
            $data['banid_info'] = $banid_info;

            $data['areas'] = (new OptService())->get_city_cader();
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new LabourService())->get_labour_gong_gonglei();
            $data['files'] = (new OptService())->get_file('labour_gong', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_gong(Request $request){
        $param = $request->post();
        $data = LabourGongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourGongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_gong', $data['id'], '工人花名册');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_gong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourGongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $banid_info = LabourBanModel::get_info_by_pk($data['data']['banid']);
        $data['banid_info'] = $banid_info;

        $data['data']['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $data['data']['gongtype']);
        $data['files'] = (new OptService())->get_file('labour_gong', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_labour_ban(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.username|ling.usertel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['username']) && !empty($param['username'])){
            $where[] = ['ling.username','like', '%'.$param['username'].'%'];
        }
        if(isset($param['usertel']) && !empty($param['usertel'])){
            $where[] = ['ling.usertel','like', '%'.$param['usertel'].'%'];
        }

        $data['data'] = LabourBanModel::alias('ling')
            ->field('ling.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_ban', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $banname = LabourGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'banid'=>$item['id']])->column('name');
                $item['bannum'] = count($banname);
                $item['banname'] = implode('/',$banname);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_ban');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入班组名称');
            }
            if((new LabourService())->check_labour_ban_name($param['name'])){
                return outputJson(4000, '班组名称已存在');
            }

            $gonger = $param['gonger'];
            unset($param['gonger']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = LabourBanModel::insertGetId($param);

                LabourGongModel::where(['comid'=>$this->userinfo['comid']])->where('id','in',$gonger)->update(['banid'=>$id]);


                (new OptService())->relation_file($files,'labour_ban',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_ban', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_ban', $id, '班组管理');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function edit_labour_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入班组名称');
            }
            if((new LabourService())->check_labour_ban_name($param['name'], $param['id'])){
                return outputJson(4000, '班组名称已存在');
            }

            $gonger = $param['gonger'];
            unset($param['gonger']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                LabourBanModel::update($param);

                LabourGongModel::where(['comid'=>$this->userinfo['comid'],'banid'=>$param['id']])->update(['banid'=>0]);
                LabourGongModel::where(['comid'=>$this->userinfo['comid']])->where('id','in',$gonger)->update(['banid'=>$param['id']]);

                (new OptService())->relation_file($files,'labour_ban',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_ban', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_ban', $param['id'], '班组管理');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourBanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['gonger_info'] = LabourGongModel::field('id,name')->where(['banid'=>$data['data']['id']])->select()->toArray();
            $data['files'] = (new OptService())->get_file('labour_ban', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_ban(Request $request){
        $param = $request->post();
        $data = LabourBanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourBanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_ban', $data['id'], '班组管理');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_ban(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourBanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $gonger = LabourGongModel::where(['banid'=>$data['data']['id']])->select()->toArray();
        foreach ($gonger as &$item){
            $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
        }
        $data['gonger'] = $gonger;

        $data['files'] = (new OptService())->get_file('labour_ban', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_labour_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ling.enginid','=', $param['enginid']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = LabourXiangModel::alias('ling')
            ->field('ling.*,e.name as enginname')
            ->join('engin e','e.id=ling.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_xiang', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zongnum'] = LabourGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where('id','in',$item['gonger'])->count();

                $item['zongban'] = LabourGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where('id','in',$item['gonger'])
                    ->where('banid','gt',0)
                    ->group('banid')
                    ->count();
                $item['zonggong'] = LabourGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where('id','in',$item['gonger'])
                    ->group('gongtype')
                    ->count();

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_xiang');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            unset($param['token']);


            $param['gonger'] = implode(',', array_unique(explode(',', $param['gonger'])));

            $exist = LabourXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$param['enginid']])->find();


            Db::startTrans();
            try {
                if($exist){
                    $param['id'] = $exist['id'];
                    LabourXiangModel::update($param);
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id'], '项目花名册');
                }else{
                    $id = LabourXiangModel::insertGetId($param);
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_xiang', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_xiang', $id, '项目花名册');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function edit_labour_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);
            $param['gonger'] = implode(',', array_unique(explode(',', $param['gonger'])));

            Db::startTrans();
            try {
                LabourXiangModel::update($param);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id'], '项目花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourXiangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['enginid_name'] = EnginModel::where(['id'=>$data['data']['enginid']])->value('name');

            $data['data']['gonger_info'] = LabourGongModel::field('id,name')
                ->where('id','in',$data['data']['gonger'])->select()->toArray();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_xiang(Request $request){
        $param = $request->post();
        $data = LabourXiangModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourXiangModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_xiang', $data['id'], '项目花名册');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_xiang(Request $request){
        $param = $request->post();
        $xiang = LabourXiangModel::where(['id'=>$param['id']])->find();
        if(!$xiang){
            return outputJson(2000,lang('code_2000'),[]);
        }

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.id','in',$xiang['gonger']];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }
        if(isset($param['banid']) && !empty($param['banid'])){
            $where[] = ['ling.banid','=', $param['banid']];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';


        $banidArr = LabourGongModel::alias('ling')->where($where)->group('ling.banid')->column('ling.banid');
        $data['banidInfo'] = LabourBanModel::field('id,name')->where('id','in',$banidArr)->select()->toArray();
        $data['is_banid'] = isset($param['banid']) ? $param['banid'] : '';

        $enginid_name = EnginModel::where(['id'=>$xiang['enginid']])->value('name');
        $data['enginid_name'] = $enginid_name;

        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*,ban.name as banid_name')
            ->leftJoin('labour_ban ban','ban.id=ling.banid')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($enginid_name){
                $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
                $item['enginid_name'] = $enginid_name;
                return $item;
            })->toArray();



        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_labour_xiangm(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourXiangModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['enginid_name'] = EnginModel::where(['id'=>$data['data']['enginid']])->value('name');

        $gonger = LabourGongModel::where('id','in',$data['data']['gonger'])->select()->toArray();
        foreach ($gonger as &$item){
            $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
        }
        $data['gonger'] = $gonger;
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_labour_yong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.isturn','=',1];
        $where[] = ['ling.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|ling.shi|ling.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ling.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ling.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shi']) && !empty($param['shi'])){
            $where[] = ['ling.shi','like', '%'.$param['shi'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ling.status','=', $param['status']];
        }


        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourYongModel::alias('ling')
            ->field('ling.*,e.name as enginname')
            ->join('engin e','e.id=ling.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_yong', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caiid_name'] = '';
                if($item['yongxing'] == '劳务分包'){
                    $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                }
                $item['yongnums'] = LabourYongListModel::where(['yongid'=>$item['id']])->count();
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_yong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_labour_yong(Request $request){
        $param = $request->post();
        $data = LabourYongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourYongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_yong', $data['id'], '用工单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_yong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourYongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);



        $list = LabourYongListModel::where(['yongid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $gonginfo = LabourGongModel::where(['id'=>$item['gongid']])->find();
            if($gonginfo){
                $item['name'] = $gonginfo['name'];
                $item['idcard'] = $gonginfo['idcard'];
            }
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_yong', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_labour_hua(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['hua.deleted_user','=', 0];
        $where[] = ['hua.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourHuaModel::alias('hua')
            ->field('hua.*,e.name as enginname,e.business,e.state,e.man_xiang,u.realname as created_user_name,
            cai.name as caiid_name,cai.number as caiid_number')
            ->join('engin e','e.id=hua.enginid')
            ->leftJoin('contract_cai cai','cai.id=hua.caiid')
            ->join('user u','u.id=hua.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_hua', 'hua'))
            ->order('hua.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['man_xiang_name'] = UserModel::get_realname_by_pk($item['man_xiang']);
                $item['jin_num'] = LabourHuaListModel::where(['huaid'=>$item['id']])->count();
                $item['tui_num'] = LabourHuaListModel::where(['huaid'=>$item['id']])
                    ->where("tuidt != '' and tuidt is not null and tuidt != '0000-00-00'")->count();
                $item['zai_num'] = $item['jin_num'] - $item['tui_num'];
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_hua');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_hua(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入成员信息');
            }



            Db::startTrans();
            try {
                $id = LabourHuaModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['huaid'] = $id;
                    LabourHuaListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'labour_hua',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_hua', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_hua', $id, '项目花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function edit_labour_hua(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入成员信息');
            }

            Db::startTrans();
            try {
                LabourHuaModel::update($param);

                LabourHuaListModel::where(['huaid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['huaid'] = $param['id'];
                    LabourHuaListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'labour_hua',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_hua', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_hua', $param['id'], '项目花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourHuaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = LabourHuaListModel::where(['huaid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['file_shen_arr'] = $item['file_shen'] = FileModel::where('id','in',$item['file_shen'])->select()->toArray();
                $item['file_shen_num'] = count($item['file_shen_arr']);

                $item['file_he_arr'] = $item['file_he'] = FileModel::where('id','in',$item['file_he'])->select()->toArray();
                $item['file_he_num'] = count($item['file_he_arr']);

                $item['file_gang_arr'] = $item['file_gang'] = FileModel::where('id','in',$item['file_gang'])->select()->toArray();
                $item['file_gang_num'] = count($item['file_gang_arr']);

                $item['file_book_arr'] = $item['file_book'] = FileModel::where('id','in',$item['file_book'])->select()->toArray();
                $item['file_book_num'] = count($item['file_book_arr']);
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('labour_hua', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_hua(Request $request){
        $param = $request->post();
        $data = LabourHuaModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourHuaModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_hua', $data['id'], '项目花名册');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_hua(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourHuaModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
        $data['engininfo'] = $engininfo;


        $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
        $data['caiidinfo'] = $caiidinfo;

        $list = LabourHuaListModel::where(['huaid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['file_shen_arr'] = $item['file_shen'] = FileModel::where('id','in',$item['file_shen'])->select()->toArray();
            $item['file_shen_num'] = count($item['file_shen_arr']);

            $item['file_he_arr'] = $item['file_he'] = FileModel::where('id','in',$item['file_he'])->select()->toArray();
            $item['file_he_num'] = count($item['file_he_arr']);

            $item['file_gang_arr'] = $item['file_gang'] = FileModel::where('id','in',$item['file_gang'])->select()->toArray();
            $item['file_gang_num'] = count($item['file_gang_arr']);

            $item['file_book_arr'] = $item['file_book'] = FileModel::where('id','in',$item['file_book'])->select()->toArray();
            $item['file_book_num'] = count($item['file_book_arr']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_hua', $data['data']['id']);

//        $list = EnginXunListModel::where(['xunid'=>$data['data']['id']])->select()->toArray();
//        foreach ($list as &$item){
//            $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
//            $item['cusname'] = count($item['filearr']);
//        }
//        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_labour_wage(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['wage.deleted_user','=', 0];
        $where[] = ['wage.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }



        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourWageModel::alias('wage')
            ->field('wage.*,e.name as enginname,e.business,e.state,e.man_xiang,u.realname as created_user_name')
            ->join('engin e','e.id=wage.enginid')
            ->join('user u','u.id=wage.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_wage', 'wage'))
            ->order('wage.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['man_xiang_name'] = UserModel::get_realname_by_pk($item['man_xiang']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_wage');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = LabourWageModel::insertGetId($param);

                (new OptService())->relation_file($files,'labour_wage',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_wage', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_wage', $id, '项目工资表');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function edit_labour_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                LabourWageModel::update($param);

                (new OptService())->relation_file($files,'labour_wage',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_wage', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_wage', $param['id'], '项目工资表');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourWageModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
            $data['engininfo'] = $engininfo;

            $data['files'] = (new OptService())->get_file('labour_wage', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_wage(Request $request){
        $param = $request->post();
        $data = LabourWageModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourWageModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_wage', $data['id'], '项目工资表');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_wage(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourWageModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
        $data['engininfo'] = $engininfo;


        $data['files'] = (new OptService())->get_file('labour_wage', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }





}
